<template>
	<view class="content">
		<view class="user-info">
			<view class="line avatar">
				<view class="value avatar-value">
					<image
						:src="userInfo.avatar ? $u.fileBaseUrl + userInfo.avatar : '../../static/images/me/default-avatar-orange.png'"
						mode="aspectFill" @click="checkAvatar"></image>
				</view>
				<view class="label avatar top-header-text">点击修改头像哦!</view>
			</view>
			<view class="line">
				<view class="label">输入昵称</view>
				<view class="value">
					<u--input placeholder="请输入昵称" v-model="userInfo.nickname" border="surround" clearable
						fontSize="24"></u--input>
					<image src="../../static/images/me/edit.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="line sex-wrap">
				<view class="label">性别</view>
				<view class=" sex-value">
					<sex :isDefault="false" v-model="userInfo.gender"></sex>
				</view>
			</view>
			<view class="line">
				<view class="label">手机号</view>
				<view class="value">
					<u--input placeholder="请输入手机号" v-model="userInfo.phone" border="surround" clearable
						fontSize="24"></u--input>
					<image src="../../static/images/me/edit.png" mode="aspectFill"></image>
				</view>
			</view>
			<view class="line">
				<view class="label">个性签名</view>
				<view class="textarea-value">
					<u--textarea v-model="userInfo.personalSign" placeholder="请输入签名" border="surround" clearable
						fontSize="24"></u--textarea>
					<image src="../../static/images/me/edit.png" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="btn-box">
			<button @click="submit" :disabled="!isChange">确认修改</button>
		</view>
	</view>
</template>

<script>
	import sex from "@/components/sex.vue"
	export default {
		data() {
			return {
				oldUserInfo: {},
				userInfo: {
					avatar: "",
					phone: "",
					nickname: "",
					gender: 'male_photography',
					personalSign: ""
				},
			}
		},
		components: {
			sex
		},
		computed: {
			// 是否改变
			isChange() {
				return JSON.stringify(this.oldUserInfo) !== JSON.stringify(this.userInfo);
			}
		},
		methods: {
			// 选择头像
			async checkAvatar() {
				let that = this;
				uni.chooseMedia({
					count: 1, //默认9
					mediaType: "image",
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					async success(res) {
						let tempFilePath = res.tempFiles[0].tempFilePath;
						console.log(tempFilePath);
						let res_ = await that.$u.api.uploadFile(tempFilePath);
						if (res_.code == 200) {
							that.$u.toast("上传成功！");
							that.userInfo.avatar = res_.fileName;
						}
					},
				});
			},
			// 提交修改
			async submit() {
				if (this.$u.test.isEmpty(this.userInfo.nickname)) return this.$u.toast("请填写昵称！");
				if (this.$u.test.isEmpty(this.userInfo.phone)) return this.$u.toast("请填写手机号！");
				if (!this.$u.test.mobile(this.userInfo.phone)) return this.$u.toast("手机号格式错误！");
				let res = await this.$u.api.getupdateUser({
					id: this.userInfo.id,
					avatar: this.userInfo.avatar,
					phone: this.userInfo.phone,
					nickname: this.userInfo.nickname,
					gender: this.userInfo.gender,
					personalSign: this.userInfo.personalSign
				});
				console.log('this.userInfo.avatar', this.userInfo.avatar);
				if (res.code == 200) {
					this.$u.toast("修改成功！");
					uni.setStorageSync("userInfo", this.userInfo);
					setTimeout(() => {
						uni.navigateBack();
					}, 1500);
				}
			},
			//获取用户信息
			// async getUserInfo() {
			// 	let res = await this.$u.api.getqueryUser();
			// 	if (res.code == 200) {
			// 		this.userInfo = res.data;
			// 		uni.setStorageSync("userInfo", res.data);
			// 	}
			// },
		},
		onLoad() {
			let userInfo = uni.getStorageSync("userInfo");
			if (userInfo) {
				this.oldUserInfo = this.$u.deepClone(userInfo);
				this.userInfo = userInfo;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;

		.user-info {
			display: flex;
			flex-direction: column;
			width: 100%;
			min-height: calc(98vh - 170rpx);
			margin: 0 auto;
			background-color: #fff;
			box-sizing: border-box;
			border-radius: 20rpx;


			.line {
				padding: 20rpx 0;

				.label {
					font-size: 28rpx;
					margin-bottom: 10rpx;

					&.avatar {
						font-size: 20rpx;
						color: #666666;
					}
				}

				.value,
				.textarea-value {

					background: #f3f3f3;
					border-radius: 6rpx;
				}

				.textarea-value {
					flex:1;
					display: flex;
					image{
						width:40rpx;
						height:40rpx;
						margin:20rpx;
					}
					::v-deep .u-textarea {
						background: #f3f3f3;

						textarea {
							font-size: 24rpx !important;
						}
					}
				}

				.value:not(:first-child) {
					display: flex;
					justify-content: space-between;
					align-items: center;

					&>image {
						width: 40rpx;
						height: 40rpx;
						margin-right: 20rpx;
					}
				}

				&.avatar {
					text-align: center;

					.value {
						background: white;

						image {
							width: 170rpx;
							height: 170rpx;
							border-radius: 50%;
						}
					}

					.avatar-value {
						background-color: white;
					}

					.top-header-text {
						color: #d5d5d5;
						margin-top: 10rpx;
					}
				}




			}

			.sex-wrap {
				display: flex;
				align-items: center;
				gap: 100rpx;

				&>.label {
					margin-bottom: 0;
				}

				.sex-value {
					background: white;
				}
			}

			& .line:last-child {
				flex: 1;
				display: flex;
				flex-direction: column;

				&>.value {
					flex: 1;
				}
			}
		}

		.btn-box {
			width: 100%;
			margin-top: 30rpx;

			button {
				width: 100%;
				height: 80rpx;
				line-height: 80rpx;
				background: $overall-color;
				border-radius: 100rpx 100rpx 100rpx 100rpx;
				font-size: 36rpx;
				color: #FFFFFF;

				&::after {
					content: none;
				}

				&[disabled] {
					background-color: #ccc;
				}
			}
		}
	}
</style>